<?php
namespace app\admin\controller;
use \think\Db;
use think\Cache;
use think\Loader;
use think\Session;
use think\Controller;
//个人会员处理
/**
 * Class Member 个人会员管理控制器
 * @package app\admin\controller
 */
class Member extends Controller
{       
    //个人会员列表
    public function index()
    {
    	$param = $this->request->param();
    	$where = [];
        //条件查询
        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['addtime'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.addtime'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.addtime'] = ['<= time', $endTime];
            }
        }
        $userName   = empty($param['userName']) ? '' : $param['end_time'];
        if (!empty($userName)) {
            $where['a.userName'] = $userName;
        }
        $userPhone   = empty($param['userPhone']) ? '' : $param['userPhone'];
        if (!empty($userPhone)) {
            $where['a.userPhone'] = $userPhone;
        }

    	$rs=db::name('users')->alias('a')
        ->join('mj_vip_order b','a.userId=b.userId','left')
        ->field('a.*,b.duration')
        ->where($where)
        ->where('userStatus',2)
        ->order('addtime desc')
        ->group('a.userId')
        ->paginate(10);
    	//dump($rs);exit;
    	$count=db::name('users')->where($where)->where('userStatus',2)->count();
    	$this->assign("count", $count);//设置过滤条件
        $this->assign("filter", $param);//设置过滤条件
    	$this->assign('arr',$rs);
        return $this->fetch();
    }

    //改变个人会员状态
    public function changeState()
    {
        $userId = input('id');
        $userStatus = input('status');
        $userStatus = $userStatus==1?'2':'1';
        $result = Db::name('users')->where('userId', $userId)->update(['status' => $userStatus]);
        if($result){
            return json(['code'=>1,'msg'=>'状态设置成功！']);
        }else {
            return json(['code'=>1,'msg'=>'状态设置失败！']);
        }
    }
}
